package cn.limbo.myandserver.utils;

import android.os.Environment;

import java.io.File;
import java.math.BigDecimal;

public class FileUtils {

    /**
     * 检查SD卡是否可用
     * @return 检查结果
     */
    public static boolean storageAvailable() {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            File sd = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
            return sd.canWrite();
        } else {
            return false;
        }
    }

    /**
     * 获取文件的大小
     *
     * @param file 文件
     * @return 文件的大小
     */
    public static String getSize(File file) {
        long size = file.length();
        double size1;
        double size2;
        //如果字节数少于1024，则直接以B为单位，否则先除于1024，后3位因太少无意义
        if (size < 1024) {
            return String.valueOf(size) + "B";
        } else {
            size1 = (double) size / 1024;
        }
        //如果原字节数除于1024之后，少于1024，则可以直接以KB作为单位
        //因为还没有到达要使用另一个单位的时候
        if (size1 < 1024) {
            return String.valueOf(new BigDecimal(size1).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()) + "KB";
        } else {
            size2 = size1 / 1024;
        }
        if (size2 < 1024) {
            // 以MB为单位
            return String.valueOf(new BigDecimal(size2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()) + "MB";
        } else {
            // 以GB为单位
            size2 = size2 / 1024;
            return String.valueOf(new BigDecimal(size2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()) + "GB";
        }
    }

    /*
        文件列表和在线预览html界面的字符串
     */
    public static final String FOLDER_HTML_PREFIX = "<!DOCTYPE html><html lang=\"zh\"><head><meta http-equiv=\"content-type\" content=\"text/html; " +
            "charset=utf-8\"/><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"><meta name=\"format-detection\" " +
            "content=\"telephone=no\"/><link rel=\"stylesheet\" href=\"../../frame/layui/css/layui.css\"><link rel=\"stylesheet\" " +
            "href=\"../../css/style.css\"><link rel=\"icon\" href=\"../../image/code.png\"><title>%1$s</title><style>.center_horizontal" +
            "{margin:0 auto;text-align:center;}*,*::after,*::before {box-sizing: border-box;margin: 0;padding: 0;}a:-webkit-any-link " +
            "{color: -webkit-link;cursor: auto;text-decoration: underline;}</style></head><body>" +
            "<h1 class=\"center_horizontal\">%2$s</h1><table class=\"layui-table\"><colgroup><col><col width=\"90\">" +
            "<col width=\"100\"></colgroup><thead><tr><th>文件名</th><th>文件类型</th><th>文件大小</th></tr></thead><tbody>";
    public static final String FOLDER_ITEM = "<tr><td><a href=\"/file/ota/%1$s\">%1$s</a></td><td>%2$s</td></tr>";
    public static final String FOLDER_VIDEO_ITEM = "<tr><td><a href=\"/file/video/%1$s\" target=\"_Blank\">%1$s</a></td><td>%2$s</td><td>%3$s</td></tr>";
    public static final String FOLDER_IMAGE_ITEM = "<tr><td><a href=\"/file/image/%1$s\" target=\"_Blank\">%1$s</a></td><td>%2$s</td><td>%3$s</td></tr>";
    public static final String FOLDER_AUDIO_ITEM = "<tr><td><a href=\"/file/audio/%1$s\" target=\"_Blank\">%1$s</a></td><td>%2$s</td><td>%3$s</td></tr>";
    public static final String FOLDER_HTML_SUFFIX = "</tbody></table></body></html>";
    public static final String VIDEO_PLAY = "<!DOCTYPE html><html lang=\"zh\"><head><meta charset=\"UTF-8\"><meta name=\"renderer\" content=\"webkit\">" +
            "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, " +
            "maximum-scale=1\"><title>视频观看</title><link rel=\"stylesheet\" href=\"../../frame/layui/css/layui.css\"><link rel=\"stylesheet\" " +
            "href=\"../../css/style.css\"><link rel=\"icon\" href=\"../../image/code.png\"></head><body><div class=\"login-main\"><header " +
            "class=\"layui-elip\">%1$s</header><div class=\"layui-elip\"><video controls width=\"%2$s\" height=\"%2$s\">" +
            "<source src=\"%3$s\" type=\"video/mp4\"><source src=\"%3$s\" type=\"video/ogg\"><source src=\"%3$s\" " +
            "type=\"video/webm\">您的浏览器版本太低，不支持 video 元素，无法播放</video></div></div></body></html>";
    public static final String IMAGE_PLAY = "<!DOCTYPE html><html lang=\"zh\"><head><meta charset=\"UTF-8\"><meta name=\"renderer\" content=\"webkit\">" +
            "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, " +
            "maximum-scale=1\"><title>图片预览</title><link rel=\"stylesheet\" href=\"../../frame/layui/css/layui.css\"><link rel=\"stylesheet\" " +
            "href=\"../../css/style.css\"><link rel=\"icon\" href=\"../../image/code.png\"></head><body><div class=\"login-main\"><header " +
            "class=\"layui-elip\">%1$s</header><div class=\"layui-elip\"><img src=\"%2$s\" alt=\"Something Wrong Happened\" height=\"%3$s\" " +
            "width=\"%3$s\"></div></div></body></html>";
    public static final String AUDIO_PLAY = "<!DOCTYPE html><html lang=\"zh\"><head><meta charset=\"UTF-8\"><meta name=\"renderer\" content=\"webkit\">" +
            "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, " +
            "maximum-scale=1\"><title>音频播放</title><link rel=\"stylesheet\" href=\"../../frame/layui/css/layui.css\"><link rel=\"stylesheet\" " +
            "href=\"../../css/style.css\"><link rel=\"icon\" href=\"../../image/code.png\"></head><body><div class=\"login-main\"><header " +
            "class=\"layui-elip\">%1$s</header><div class=\"layui-elip\"><audio controls><source src=\"%2$s\" type=\"audio/ogg\">" +
            "<source src=\"%2$s\" type=\"audio/mpeg\">您的浏览器版本太低，不支持 audio 元素，无法播放</audio></div></div></body></html>";
}